package com.lmb.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lmb.bean.AddressBook;
import com.lmb.bean.R;
import com.lmb.dao.AddressBookDao;
import com.lmb.service.AddressBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @projectName: reggie
 * @package: com.lmb.controller
 * @className: AddressBookController
 * @author: Eric
 * @description: TODO
 * @date: 2022/9/21 20:03
 */
@RestController
@RequestMapping("/addressBook")
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;




    @GetMapping("/list")
    public R addressBookList(HttpSession session){
        Long userId = (Long) session.getAttribute("user");
        LambdaQueryWrapper<AddressBook> addressBookLambdaQueryWrapper = new LambdaQueryWrapper<>();
        addressBookLambdaQueryWrapper.eq(AddressBook::getUserId,userId);
        List<AddressBook> addressBooks = addressBookService.list(addressBookLambdaQueryWrapper);

        return R.success(addressBooks);
    }

    @PostMapping
    public R addAddressBook(@RequestBody AddressBook addressBook,HttpSession session){
        Long userId = (Long)session.getAttribute("user");

        addressBook.setUserId(userId);
        addressBook.setCreateUser(userId);
        addressBook.setCreateTime(LocalDateTime.now());

        addressBook.setUpdateUser(userId);
        addressBook.setUpdateTime(LocalDateTime.now());

        boolean save = addressBookService.save(addressBook);
        return save?R.success("添加地址成功"):R.error("添加地址失败");
    }

    @PutMapping("/default")
    public R defaultAdressBook(@RequestBody AddressBook addressBook ,HttpSession session){
        Long id = addressBook.getId();
        Long userId = (Long)session.getAttribute("user");
        LambdaQueryWrapper<AddressBook> addressBookLambdaQueryWrapper = new LambdaQueryWrapper<>();
        addressBookLambdaQueryWrapper.eq(AddressBook::getUserId,userId);
        addressBookLambdaQueryWrapper.eq(AddressBook::getIsDefault,1);
        AddressBook one = addressBookService.getOne(addressBookLambdaQueryWrapper);
        if (one!=null){
            one.setUpdateTime(LocalDateTime.now());
            one.setUpdateUser(userId);
            one.setIsDefault(0);
            addressBookService.updateById(one);
        }
        addressBook.setUpdateTime(LocalDateTime.now());
        addressBook.setUpdateUser(userId);
        addressBook.setIsDefault(1);
        addressBookService.updateById(addressBook);
        return R.success("修改成功");
    }


    @GetMapping("/default")
    public R AdressBookIsDefault(HttpSession session){
        Long userId = (Long)session.getAttribute("user");
        LambdaQueryWrapper<AddressBook> addressBookLambdaQueryWrapper = new LambdaQueryWrapper<>();
        addressBookLambdaQueryWrapper.eq(AddressBook::getUserId,userId);
        addressBookLambdaQueryWrapper.eq(AddressBook::getIsDefault,1);
        AddressBook one = addressBookService.getOne(addressBookLambdaQueryWrapper);
        System.out.println(one);
        return R.success(one);
    }

}
